import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../core/theme/theme.dart';
import '../../../domain/providers/auth_provider.dart';
import '../../../domain/providers/upload_queue_provider.dart';
import '../../../core/di/service_locator.dart';
import '../../../domain/services/upload_task_factory.dart';
import 'dashboard_screen.dart';
import 'files_screen.dart';
import 'profile_screen.dart';
import 'upload_list_screen.dart';
import 'widgets/upload_fab.dart';
import 'widgets/upload_nav_item.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  // 当前选中的页面索引
  int _currentIndex = 0;

  // 页面列表
  final List<Widget> _pages = [
    const DashboardScreen(),
    const FilesScreen(),
    const UploadListScreen(),
    const ProfileScreen(),
  ];

  // 页面标题
  final List<String> _titles = ['首页', '文件', '上传', '我的'];

  @override
  Widget build(BuildContext context) {
    final authProvider = Provider.of<AuthProvider>(context);
    final themeProvider = Provider.of<ThemeProvider>(context);
    final uploadQueueProvider = Provider.of<UploadQueueProvider>(context);

    // 获取上传任务状态
    final processingTasksCount = uploadQueueProvider.processingTasks.length;
    final hasTasks = uploadQueueProvider.tasks.isNotEmpty;

    return Scaffold(
      appBar: AppBar(
        title: Text(_titles[_currentIndex]),
        actions: [
          // 主题切换按钮
          IconButton(
            icon: Icon(
              themeProvider.isDarkMode ? Icons.light_mode : Icons.dark_mode,
            ),
            onPressed: () {
              themeProvider.toggleTheme();
            },
          ),
        ],
      ),
      // 根据当前索引显示对应页面
      body: _pages[_currentIndex],
      // 底部导航栏
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        items: [
          const BottomNavigationBarItem(
            icon: Icon(Icons.home_outlined),
            activeIcon: Icon(Icons.home),
            label: '首页',
          ),
          const BottomNavigationBarItem(
            icon: Icon(Icons.folder_outlined),
            activeIcon: Icon(Icons.folder),
            label: '文件',
          ),
          // 使用封装后的上传导航项组件
          UploadNavItem(
            hasTasks: hasTasks,
            processingTasksCount: processingTasksCount,
          ).build(),
          const BottomNavigationBarItem(
            icon: Icon(Icons.person_outline),
            activeIcon: Icon(Icons.person),
            label: '我的',
          ),
        ],
      ),
      // 添加浮动操作按钮，用于上传文件
      floatingActionButton:
          _currentIndex == 1 || _currentIndex == 2
              ? UploadFab(
                onUploadComplete: (int taskCount) {
                  if (taskCount > 0) {
                    setState(() {
                      _currentIndex = 2; // 切换到上传页面
                    });
                  }
                },
                marginBottom: 42, // 向上移动按钮
                marginRight: 10, // 向左移动按钮
              )
              : null,
      // 设置浮动按钮的位置，调整其在屏幕上的位置
      floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
    );
  }
}
